﻿using Microsoft.SharePoint;
using Nintex.Workflow;
using Nintex.Workflow.Activities.Adapters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Workflow.ComponentModel;

namespace NintexX.Actions.InvokeNintexXAction
{
    public class InvokeNintexXActionAdapter : GenericRenderingAction
    {
        private const string TargetAssemblyPropertyName = "TargetAssembly";
        private const string TargetTypePropertyName = "TargetType";
        private const string InputParamPropertyName = "InputParam";

        public override NWActionConfig GetDefaultConfig(GetDefaultConfigContext context)
        {
            NWActionConfig config = new NWActionConfig(this);

            config.Parameters = new ActivityParameter[3];

         
            //define the parameters that the user can configure for this action.
            config.Parameters[0] = new ActivityParameter();
            config.Parameters[0].Name = TargetAssemblyPropertyName;
            config.Parameters[0].PrimitiveValue = new PrimitiveValue();
            config.Parameters[0].PrimitiveValue.Value = string.Empty;
            config.Parameters[0].PrimitiveValue.ValueType = SPFieldType.Text.ToString();

            config.Parameters[1] = new ActivityParameter();
            config.Parameters[1].Name = TargetTypePropertyName;
            config.Parameters[1].PrimitiveValue = new PrimitiveValue();
            config.Parameters[1].PrimitiveValue.Value = string.Empty;
            config.Parameters[1].PrimitiveValue.ValueType = SPFieldType.Text.ToString();

            config.Parameters[2] = new ActivityParameter();
            config.Parameters[2].Name = InputParamPropertyName;
            config.Parameters[2].PrimitiveValue = new PrimitiveValue();
            config.Parameters[2].PrimitiveValue.Value = string.Empty;
            config.Parameters[2].PrimitiveValue.ValueType = SPFieldType.Text.ToString();

            //set the default label for the action.
            config.TLabel = ActivityReferenceCollection.FindByAdapter(this).Name;
            return config;

        }

        public override bool ValidateConfig(ActivityContext context)
        {
            //Add logic to validate the configuration here.
            bool isValid = true;

            Dictionary<string, ActivityParameterHelper> parameters = context.Configuration.GetParameterHelpers();

           

            if (!parameters[TargetAssemblyPropertyName].Validate(typeof(string), context))
            {
                isValid &= false;
                validationSummary.AddError("Target Assembly", ValidationSummaryErrorType.CannotBeBlank);
            }

            if (!parameters[TargetTypePropertyName].Validate(typeof(string), context))
            {
                isValid &= false;
                validationSummary.AddError("Target Type", ValidationSummaryErrorType.CannotBeBlank);
            }

            return isValid;
        }

        public override CompositeActivity AddActivityToWorkflow(PublishContext context)
        {
            //Create an instance of the Activity and set its properties based on config. Add it to the parent activity
            Dictionary<string, ActivityParameterHelper> parameters = context.Config.GetParameterHelpers();

            InvokeNintexXActionActivity activity = new InvokeNintexXActionActivity();
           
            parameters[TargetAssemblyPropertyName].AssignTo(activity, InvokeNintexXActionActivity.TargetAssemblyProperty, context);
            parameters[TargetTypePropertyName].AssignTo(activity, InvokeNintexXActionActivity.TargetTypeProperty, context);
            parameters[InputParamPropertyName].AssignTo(activity, InvokeNintexXActionActivity.InputParamProperty, context);

            activity.SetBinding(InvokeNintexXActionActivity.__ContextProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__context));
            activity.SetBinding(InvokeNintexXActionActivity.__ListItemProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__item));
            activity.SetBinding(InvokeNintexXActionActivity.__ListIdProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__list));

            ActivityFlags f = new ActivityFlags();
            f.AddLabelsFromConfig(context);
            f.AssignTo(activity);

            context.ParentActivity.Activities.Add(activity);
            return null;
        }

        public override NWActionConfig GetConfig(RetrieveConfigContext context)
        {
            //Read the properties from the context.ACtivity and update the values in the NWActionConfig
            NWActionConfig config = this.GetDefaultConfig(context);

            Dictionary<string, ActivityParameterHelper> parameters = config.GetParameterHelpers();
           
            parameters[TargetAssemblyPropertyName].RetrieveValue(context.Activity, InvokeNintexXActionActivity.TargetAssemblyProperty, context);
            parameters[TargetTypePropertyName].RetrieveValue(context.Activity, InvokeNintexXActionActivity.TargetTypeProperty, context);
            parameters[InputParamPropertyName].RetrieveValue(context.Activity, InvokeNintexXActionActivity.InputParamProperty, context);

            return config;
        }

        public override ActionSummary BuildSummary(ActivityContext context)
        {
            Dictionary<string, ActivityParameterHelper> parameters = context.Configuration.GetParameterHelpers();
            return new ActionSummary("Invoke Action: {0}", parameters[TargetTypePropertyName].Value);
        }
    }
}
